package com.sysolve.betterCity.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sysolve.betterCity.common.Help;
import com.sysolve.betterCity.service.CalcHotArea;
import com.sysolve.betterCity.service.DailyScheduler;

/**
 * Servlet implementation class for Servlet: RetrieveHotArea
 *
 */
 public class RetrieveHotArea extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	 
	public static CalcHotArea calc = new CalcHotArea(500,3);
	public static boolean init = DailyScheduler.schedule(calc);
	 
	public RetrieveHotArea() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");  
		response.setCharacterEncoding("utf-8");
		
        PrintWriter out = response.getWriter();
        out.println("<p>如果你去一个地方有活动，我去一个地方有活动，大家都去一个地方有活动，说明这是一个繁华的地方。如果一个地方从没有人去，到很多人去，这就是<strong>繁华圈</strong>的产生。让我们一起来记录城市的发展过程，来体会城市如何让生活更美好。</p>");
        out.println("<p>BetterCity会根据大家的活动记录，在每天的0点整、6点整、12点整、18点整自动计算城市<strong>繁华圈</strong>。</p>");

        if ("calc".equals(request.getParameter("command"))) {
        	int r = Help.getInt(request.getParameter("r"));
        	int h = Help.getInt(request.getParameter("h"));
        	if (r>0) calc.radius = r;
        	if (h>0) calc.heightLevel = h;
        	calc.startCalc();
        } else {
        
	        if (calc.hotAreas==null) {
	        	out.println("<p><strong>正在根据大家的活动记录，计算繁华圈，请稍后刷新。</strong></p>"); 
	        	calc.startCalc();
	        } else {
	        	out.println(calc.calcResult);
	    		
	        }
        }
        out.close();
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}   	  	    
}